<template>
  <div>
    <!-- 添加或修改资产清单表对话框 -->
    <el-dialog ref="editDialog" v-model="openFlag" title="添加派发单" style="max-height: 86vh; width: 70vw;" append-to-body>
      <el-scrollbar always="true" max-height="60vh">
        <div class="ui_el_dialog_body">
          <el-form ref="form" :model="formData" :rules="rules" label-width="108px" class="dialog-form-two">
            <el-row>
              <el-text class="from-title-style">基本信息</el-text>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="领用人" prop="targetUserId">
                  <user-select v-model="formData.targetUserId" v-model:dept-name="formData.deptName" placeholder="请输入"></user-select>
                </el-form-item>
              </el-col><el-col :span="8">
                <el-form-item label="领用部门" prop="targetUserId">
                  <el-input v-model="formData.deptName" placeholder="领用部门" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="派发时间" prop="contactPhone">
                  <el-date-picker style="width: 100%;" placeholder="派发时间" v-model.trim="formData.targetDate" value-format="YYYY-MM-DD"></el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model.trim="formData.remark" type="textarea" :rows="5" placeholder="请输入备注" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>

          <el-row>
            <el-text class="from-title-style">资产列表</el-text>
          </el-row>
          <choose-table ref="chooseTable"></choose-table>
        </div>
      </el-scrollbar>
      <template #footer>
        <div class="ui_el_dialog_footer">
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="sub">提交</el-button>
          <el-button @click="close">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>


import api from "@/api/asset/apply.js";
import UserSelect from "@/components/User/UserSelect.vue";
import ChooseTable from "@/components/Asset/ChooseTable.vue";

const emit = defineEmits(['sub']);

const props = defineProps({
  applyType: {
    type: String,
    required: true,
  },
  applyTypeName: {
    type: String,
    required: true,
  },
})

// 是否编辑
const isEdit = ref(false);

const applyDataInit = {
  id: null,
  applyType: props.applyType,
  applyTypeName: props.applyTypeName,
  remark: null,
}

const applyData = reactive({ ...applyDataInit });

const formDataInit = {
  targetUserId: null,
  remark: null
}

const tableData = reactive({
  loading: false,
  list: [], // 表格列表数据
});


const openFlag = ref(false);
const form = ref();
const chooseTable = ref()

const formData = reactive({ ...formDataInit })

const rules = reactive({
  targetUserId: {
    required: true, message: '不能为空', trigger: 'blur',
  }
})

function reset() {
  Object.assign(applyData, applyDataInit);
  Object.assign(formData, formDataInit);
  tableData.list = [];
}

function open(row) {
  openFlag.value = true;
  reset();
  if (row) {
    api.detail(row).then(response => {
      Object.assign(applyData, response);
      const resFormData = response.formData;
      if (resFormData) {
        Object.assign(formData, resFormData);
      }

      let relItems = formData.items;
      if (!relItems) {
        relItems = []
      }
      chooseTable.value.setList(relItems)
    })
    isEdit.value = true;
  } else {
    isEdit.value = false;
  }
}

function close() {
  openFlag.value = false;
}

function getParams() {
  const apply = { ...applyData };
  apply.applyType = props.applyType;
  apply.applyTypeName = props.applyTypeName;
  formData.items = chooseTable.value.getList();
  apply.formData = JSON.stringify(formData);
  return {
    edit: isEdit.value,
    apply: apply
  }
}

function check() {
  return new Promise((resolve, reject) => {
    form.value.validate(async (valid) => {
      if (valid) {
        resolve();
      } else {
        reject();
      }
    });
  })
}

/** 保存 */
function save() {
  check().then(() => {
    api.save(getParams()).then(response => {
      emit("sub");
      close();
    });
  });
}

function sub() {
  check().then(() => {
    api.saveAndSub(getParams()).then(response => {
      emit("sub");
      close();
    });
  });

}

defineExpose({
  open
})

</script>
